<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/**
 * Override site URL function
 */
class MY_Controller extends CI_Controller {

	function __construct()
	{
		parent::__construct();
	}
	
	function check_logged_in()
	{
		if($this->current_user_name()===FALSE)
		{
			$this->session->set_userdata('divert_after_login', current_url());
			redirect('login');
		}
	}
	
	function current_user()
	{
		return $this->session->current_user();
	}	
	function current_user_id()
	{
		$u = $this->session->current_user();
		if($u)
		{
			return $u->iduser;
		}
		else
		{
			return 0;
		}		
	}	
	
	function current_user_name()
	{
		return $this->session->current_user_name();
	}	
	/*
	function current_user()
	{
		$username = $this->session->current_user_name();
		if($username)
		{
			return $this->users->load_by_username($username_to_load);
		}
		else
		{
			return FALSE;
		}
	}
	*/
	
	function ShowHeader($target = '', $additional_script_files = array(), $additional_css_files = array(), $create_page_split = true)
	{
		$userInfo = $this->session->userdata('userinfo');
		$idcountry = $this->session->userdata('country_by_ip');
		$tags = null;
		
		$data['target'] = $target;
		$data['taglist'] = '';
		$additional_script_files[] = 'scripts.js';
		$additional_css_files[] = 'ox.css';
		$data['script_files'] = $additional_script_files;
		$data['css_files'] = $additional_css_files;
		if($userInfo === FALSE) 
		{ 
			$tags = $this->targets->load_country_targets($idcountry); 
		}
		else
		{ 
			$tags = $this->targets->load_users_targets($userInfo, $idcountry); 
		}

		if(is_array($tags))
		{
			foreach ($tags as $taginfo)
			{
				$data['taglist'] .= '<div class="header_category_links">'.
									'<a href="'.site_url('target/'
									.urlencode($taginfo->name)).'">'.
									$taginfo->name.'</a></div>';
			}
		}
        
        $data['create_page_split'] = $create_page_split;

		$this->load->view('header', $data);
	}
	function ShowFooter($target = '')
	{
		$userInfo = $this->session->userdata('userinfo');
		$idcountry = $this->session->userdata('country_by_ip');
		$tags = null;
		
		$data['target'] = $target;
		$data['taglist'] = array();
		if($userInfo === FALSE) 
		{ 
			$tags = $this->targets->load_country_targets($idcountry); 
		}
		else
		{ 
			$tags = $this->targets->load_users_targets($userInfo, $idcountry); 
		}

		if(is_array($tags))
		{
			foreach ($tags as $taginfo)
			{
				$data['taglist'][] = array('url'=>'target/'.urlencode($taginfo->name),
                                                'text'=>$taginfo->name);
			}
		}

		$this->load->view('footer', $data);
	}
    
    function Box($title, $text)
    {
        /* @var $target OxTarget */
		$b = array ("style"=>"", "title"=>$title, "text"=>$text);
        return $b;
    }
    function BoxRecent($target)
    {
        if(isset($target['name']))
        {
            /* @var $target OxTarget */
            $b = array ("box_view"=>"recent",
                    "style"=>"", "title"=>"Recent", "supertitle"=> $target['name'],
                                    "articles"=>OxArticle::get_random_articles(4));
        }
        else
        {
            $b = array ("box_view"=>"recent",
                    "style"=>"", "title"=>"Recent", "supertitle"=> '',
                                    "articles"=>OxArticle::get_random_articles(4));
        }
        return $b;
    }
    function BoxRecentRelated($article)
    {
        /* @var $article OxArticle */
		$b = array ("box_view"=>"recent",
                        "style"=>"", "title"=>"Recent related", "supertitle"=> '',
				        "articles"=>OxArticle::get_random_articles(2));
        return $b;
    }
    function BoxSuggestedReading($article)
    {
        /* @var $article OxArticle */
		$b = array ("box_view"=>"suggested",
                        "style"=>"", "title"=>"Suggested", "supertitle"=> '',
				        "articles"=>OxArticle::get_random_articles(2));
        return $b;
    } 
    function BoxFeatured()
    {
        $b = array ("box_view"=>"article",
                        "style"=>"background:#eecccc", "title"=>"Featured Article", 
				        "article"=>OxArticle::get_random_article());
        return $b;
    } 
    function BoxStory($story)
    {
        $b = array ("box_view"=>"story",
            "style"=>"", "title"=>"The whole story", 
            "story"=>$story);
        return $b;
    }     
    function UserFeed()
    {
        $feeds = "";
        
        $feedtypes = array();
        $feedtypes[] = "<b><name></b> just wrote <article title>";
        $feedtypes[] = "<b>New article in story</b> '<story>': <article title> by <author>";
        $feedtypes[] = "<b>New article in view</b> '<view>': <article title> by <author>";
        
        for($i=0;$i<5;$i++)
        {
            $text = $feedtypes[rand(0, 2)];
            
            $rand_art = OxArticle::get_random_article();
            $text = str_replace("<article title>", $rand_art->article_title_link(), $text);
            $text = str_replace("<author>", $rand_art->signature, $text);
            $text = str_replace("<view>", "<a>Politics</a>", $text);
            $text = str_replace("<story>", "<a>Libya uprising</a>", $text);
            if($rand_art->organization)
            {
                $text = str_replace("<name>", $rand_art->organization->name, $text);
            }
            else 
            {
                 $text = str_replace("<name>", $rand_art->user->name_link(), $text);
            }
            
            $feeds .= '<div class="box_feed">'. $text .'</div>';
        }
        
        
		$b = array ("box_view"=>"generic",
                        "style"=>"", "title"=>"My feed", 
				        "text"=>$feeds);
        return $b;
    }
}
